package com.qmhl.toolposition.activity

import android.content.Intent
import android.net.Uri
import android.os.Bundle
import androidx.lifecycle.lifecycleScope
import com.hjq.toast.Toaster
import com.lxj.xpopup.XPopup
import com.qmhl.toolposition.api.UserService
import com.qmhl.toolposition.base.BaseActivity
import com.qmhl.toolposition.databinding.ActivityFeedbackBinding
import kotlinx.coroutines.launch


class FeedbackActivity : BaseActivity() {

    private lateinit var binding: ActivityFeedbackBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityFeedbackBinding.inflate(layoutInflater)
        setContentView(binding.root)
    }

    override fun initData() {
        super.initData()

    }


    override fun initListener() {
        super.initListener()

        binding.ivBack.setOnClickListener {
            finish()
        }

        binding.llContainerQq.setOnClickListener {
            setContact()


        }
        binding.btnEntry.setOnClickListener {
            setContact()

        }

        binding.llContainerFeedback.setOnClickListener {
            startActivity(Intent(this, CommitFeedbackActivity::class.java))
        }
        binding.btnFeedback.setOnClickListener {
            startActivity(Intent(this, CommitFeedbackActivity::class.java))
        }
    }

    private fun setContact() {

        lifecycleScope.launch {
            var content = UserService.create()
                .isVip()
            if (content.code == 200) {
                val isVip = content.data
                if (isVip) {
                    getContact()
                } else {
                    XPopup.Builder(this@FeedbackActivity).asConfirm(
                        "提醒", "此功能仅对VIP提供"
                    ) { }
                        .show()
                }
            } else {
                Toaster.showShort(content.msg)
            }
        }


    }

    fun getContact() {
        lifecycleScope.launch {
            var content = UserService.create()
                .qqContact()
            if (content.code == 200) {
                var qqContact = content.data.qqContact
                val url = "mqqwpa://im/chat?chat_type=wpa&uin=${qqContact}"
                val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
                startActivity(intent)
            } else {
                Toaster.showShort(content.msg)
            }
        }
    }


}